<?php
// +----------------------------------------------------------------------
// | 关注方法服务层
// +----------------------------------------------------------------------
// | 主要服务于关注数据处理
// +----------------------------------------------------------------------
// | 错误编码头 207xxx
// +----------------------------------------------------------------------
namespace common\service;

use common\model\FollowModel;

class FollowService
{

    /**
     * 关注或者取消关注当前项
     * @param $followStatus
     * @param $param
     * @return array
     * @throws \Exception
     */
    public function followItem($followStatus, $param)
    {
        $followModel = model(FollowModel::class);
        if ($followStatus == "yes") {
            $followModel->where($param)->delete();
            return success_response('Item unfollowed successfully! ', ["follow_status" => "no", "follow_status_name" => 'Follow']);
        } else {
            $followModel->add($param);
            return success_response('Item Followed successfully! ', ["follow_status" => "yes", "follow_status_name" => 'UnFollow']);
        }
    }

    /**
     * 获取当前项目被关注状态
     * @param $moduleId
     * @param $linkId
     * @return array
     */
    public function getItemFollowStatus($userId, $moduleId, $linkId)
    {
        $followModel = model(FollowModel::class);
        $count = $followModel->where(["link_id" => $linkId, "module_id" => $moduleId, "user_id" => $userId])->count();
        if ($count > 0) {
            return ["follow_status" => "yes", "follow_status_name" => 'Unfollow'];
        } else {
            return ["follow_status" => "no", "follow_status_name" => 'Follow'];
        }
    }
}
